<template>
  <el-form ref="form" label-width="100px" :rules="rules" :model="form">
    <el-form-item label="主题" prop="theme">
      <el-input v-model="form.theme" maxlength="20" />
    </el-form-item>
    <el-row>
      <el-col :span="12">
        <el-form-item label="到期时间">
          <el-date-picker
            v-model="form.startDate"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="被分配人">
          <el-input v-model="form.owner" disabled />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="名称">
          <el-input v-model="form.name" disabled />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="相关项">
          <el-input v-model="form.relatedItems" disabled />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row style="text-align:right">
      <el-button type="primary" :loading="loading" @click="submit">保存</el-button>
    </el-row>

  </el-form>
</template>
<script>
import { addTask } from '@/api/task'
import dayjs from 'dayjs'
export default {
  name: 'TaskForm',
  props: {
    data: {
      type: Object,
      default: () => {
        return {}
      }
    },
    type: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      loading: false,
      form: {
        'assignerId': '',
        'name': '',
        assigner: '',
        'relatedItems': '',
        'startDate': dayjs().format('YYYY-MM-DD'),
        'theme': '',
        relatedId: ''
      },
      rules: {
        theme: [
          { required: true, message: '请输入主题', trigger: 'blur' }
        ]

      }
    }
  },

  mounted() {
    this.form.name = this.data.name
    this.form.relatedItems = this.data.relateName
    this.form.relatedId = this.data.relateId
    this.form.assigner = this.USER.userName
    this.form.type = this.type
    this.form.owner = this.USER.nickName
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        this.loading = true
        if (valid) {
          addTask(this.form).then(res => {
            this.loading = false
            this.$emit('success')
            this.$message({
              type: 'success',
              message: '添加成功'
            })
          }).catch(ex => {
            this.loading = false
            this.$message({
              type: 'error',
              message: ex.message || '添加失败'
            })
          })
        }
      })
    }
  }
}
</script>
